<template>
  <el-dialog
    :title="!dataForm.teacherId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="照片" prop="faceImg">
      <el-input v-model="dataForm.faceImg" placeholder="照片"></el-input>
    </el-form-item>
    <el-form-item label="账号" prop="account">
      <el-input v-model="dataForm.account" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="等级"></el-input>
    </el-form-item>
    <!-- <el-form-item label="添加时间" prop="addTime">
      <el-input v-model="dataForm.addTime" placeholder="添加时间"></el-input>
    </el-form-item> -->
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          teacherId: 0,
          faceImg: '',
          account: '',
          username: '',
          phone: '',
          email: '',
          level: '',
          addTime: '',
          password: ''
        },
        dataRule: {
          faceImg: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '等级不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.teacherId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.teacherId) {
            this.$http({
              url: this.$http.adornUrl(`/wx/teacher/info/${this.dataForm.teacherId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.faceImg = data.teacher.faceImg
                this.dataForm.account = data.teacher.account
                this.dataForm.username = data.teacher.username
                this.dataForm.phone = data.teacher.phone
                this.dataForm.email = data.teacher.email
                this.dataForm.level = data.teacher.level
                this.dataForm.addTime = data.teacher.addTime
                this.dataForm.password = data.teacher.password
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wx/teacher/${!this.dataForm.teacherId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'teacherId': this.dataForm.teacherId || undefined,
                'faceImg': this.dataForm.faceImg,
                'account': this.dataForm.account,
                'username': this.dataForm.username,
                'phone': this.dataForm.phone,
                'email': this.dataForm.email,
                'level': this.dataForm.level,
                'addTime': this.dataForm.addTime,
                'password': this.dataForm.password
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
